package com.jcl.recruit.crawler.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.channel.logic.ChannelNetLogic;
import com.jcl.recruit.channel.model.ChannelNet;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.constants.ChannelSourceConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CrawlerAccountService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/22 13:47
 * @Version 1.0
 **/
@Service
public class CrawlerAccountService {

    private static final Logger logger = LoggerFactory.getLogger(CrawlerAccountService.class);

    @Autowired
    private ChannelNetLogic channelNetLogic;


    public PageResult crawlerAccount(String corpCode, String source, Integer curPage, Integer pageSize){

        List<Map<String, Object>> tempList = new ArrayList<>();

        if(StringUtils.isEmpty(corpCode)){
            corpCode = null;
        }
        if(StringUtils.isEmpty(source)){
            source = null;
        }
        curPage = curPage == null ? 1 : curPage;
        pageSize = pageSize == null ? 50 : pageSize;
        PageHelper.startPage(curPage, pageSize);
        List<ChannelNet> netList = channelNetLogic.findList(corpCode, source);
        PageInfo info = new PageInfo(netList);

        if(CollectionUtils.isNotEmpty(netList)){
            for(ChannelNet channelNet: netList){
                Map<String, Object> map = new HashMap<>();
                map.put("account", channelNet.getAccountName());
                map.put("username", channelNet.getAccount());
                map.put("password", channelNet.getPassword());
                map.put("corpCode", channelNet.getCorpCode());
                map.put("siteCode", ChannelSourceConstant.crawlerSourceMap.get(channelNet.getChannelType()));
                tempList.add(map);
            }
        }

        PageResult result = new PageResult(curPage, pageSize, info.getTotal(), tempList);
        return result;
    }
}
